package com.example.composemodifiterdemo

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountBox
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

/**
 * @auth: njb
 * @date: 2022/12/18 23:38
 * @desc:
 */
class TextFixedSampleActivity : androidx.activity.ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TextFixedSample()
        }
    }

    @Preview
    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    fun TextFixedSample() {
        var username by remember { mutableStateOf("") }
        var password by remember { mutableStateOf("") }
        Column {
            Row {
                TextField(
                    modifier = Modifier
                        .padding(horizontal = 10.dp)
                        .background(Color.White, CircleShape)
                        .height(100.dp)
                        .fillMaxWidth(),
                    value = username,
                    onValueChange = {
                        username = it
                    },
                    label = { Text("用户名") },
                    leadingIcon = {
                        Icon(
                            imageVector = Icons.Filled.AccountBox,
                            contentDescription = stringResource(
                                id = R.string.description
                            )
                        )
                    }
                )
            }
            Row() {
                TextField(
                    modifier = Modifier
                        .padding(horizontal = 10.dp)
                        .background(Color.White, CircleShape)
                        .height(100.dp)
                        .fillMaxWidth(),
                    value = password,
                    onValueChange = {
                        password = it
                    },
                    label = { Text("密码") },
                    trailingIcon = {
                        IconButton(onClick = { /*TODO*/ }) {
                            Icon(
                                painter = painterResource(id = R.drawable.ic_baseline_visibility_24),
                                contentDescription =
                                stringResource(id = R.string.description)
                            )
                        }
                    }
                )
            }
            Row() {
                //OutlinedTextField
                OutlinedTextField(
                    modifier = Modifier
                        .padding(horizontal = 10.dp)
                        .background(Color.White, CircleShape)
                        .height(100.dp)
                        .fillMaxWidth(),
                    value = username,
                    onValueChange = {
                        username = it
                    },
                    label = { Text("用户名") },
                    leadingIcon = {
                        Icon(
                            imageVector = Icons.Filled.AccountBox,
                            contentDescription = stringResource(
                                id = R.string.description
                            )
                        )
                    }
                )
            }
            var text by remember { mutableStateOf("") }
            Row() {
                //仿B站风格搜索框
                Box(
                    modifier = Modifier
                        .background(Color(0xFFD3D3D3))
                        .padding(50.dp)
                ) {
                    BasicTextField(
                        value = text,
                        onValueChange = { text = it },
                        decorationBox = {
                            Row(
                                verticalAlignment = Alignment.CenterVertically,
                                modifier = Modifier.padding(vertical = 2.dp, horizontal = 8.dp)
                            ) {
                                Icon(imageVector = Icons.Filled.Search, contentDescription = "")
                                Box(
                                    modifier = Modifier
                                        .padding(horizontal = 10.dp)
                                        .weight(1f),
                                    contentAlignment = Alignment.CenterStart
                                ) {
                                    if (text.isEmpty()) {
                                        Text(
                                            text = "输入点东西看看吧~",
                                            style = TextStyle(Color(0, 0, 0, 128))
                                        )
                                    }
                                    it()
                                }
                                if (text.isNotEmpty()) {
                                    IconButton(
                                        onClick = { text = "" },
                                        modifier = Modifier.size(16.dp)
                                    ) {
                                        Icon(
                                            imageVector = Icons.Filled.Close,
                                            contentDescription = ""
                                        )
                                    }
                                }
                            }
                        },
                        modifier = Modifier
                            .padding(horizontal = 10.dp)
                            .background(Color.White, CircleShape)
                            .height(60.dp)
                            .fillMaxWidth()
                    )
                }
            }
            Box(
                modifier = Modifier
                    .background(Color(0xFFFFFFFF))
                    .padding(80.dp)
                    .fillMaxSize()
            ) {
                BasicTextField(
                    value = text,
                    onValueChange = { text = it },
                    decorationBox = {
                        Row(
                            verticalAlignment = Alignment.CenterVertically,
                            modifier = Modifier.padding(vertical = 2.dp, horizontal = 8.dp)
                        ) {
                            Icon(imageVector = Icons.Filled.Search, contentDescription = "")
                            Box(
                                modifier = Modifier
                                    .padding(horizontal = 10.dp)
                                    .weight(1f),
                                contentAlignment = Alignment.CenterStart
                            ) {
                                if (text.isEmpty()) {
                                    Text(
                                        text = "百度一下~",
                                        style = TextStyle(Color(0, 0, 0, 128))
                                    )
                                }
                                it()
                            }
                            if (text.isNotEmpty()) {
                                IconButton(
                                    onClick = { text = "" },
                                    modifier = Modifier.size(16.dp)
                                ) {
                                    Icon(
                                        imageVector = Icons.Filled.Close,
                                        contentDescription = ""
                                    )
                                }
                            }
                        }
                    },
                    modifier = Modifier
                        .padding(horizontal = 10.dp)
                        .background(Color.Red, CircleShape)
                        .height(100.dp)
                        .fillMaxWidth()
                )
            }
        }
    }
}